package net.hn.hnms.biz.support.service.impl;


import net.hn.hnms.biz.support.company.domain.dto.RiskStatisticsRockBurstDto;
import net.hn.hnms.biz.support.company.domain.vo.ChartVo;
import net.hn.hnms.biz.support.company.domain.vo.ChartXTwoVo;
import net.hn.hnms.biz.support.company.domain.vo.NowDetectionRockBurstVo;
import net.hn.hnms.biz.support.company.domain.vo.TodayAlarmVo;
import net.hn.hnms.biz.support.constant.ConstantSupport;
import net.hn.hnms.biz.support.domain.BoreholealarmAbnormalPo;
import net.hn.hnms.biz.support.group.domain.vo.AbscissdataAlarmListVo;
import net.hn.hnms.biz.support.group.domain.vo.AnchordataAlarmListVo;
import net.hn.hnms.biz.support.group.domain.vo.SupportdataAlarmListVo;
import net.hn.hnms.biz.support.service.RockBurstService;
import net.hn.hnms.biz.support.service.RoofService;
import net.hn.hnms.sys.common.core.constant.Constants;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.exception.HuaNengException;
import net.hn.hnms.sys.common.core.utils.StringUtils;
import net.hn.hnms.sys.common.redis.domain.SysOrganizationVo;
import net.hn.hnms.sys.common.redis.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 公司端-冲击地压的接口实现类
 */
@Service
public class RockBurstServiceImpl implements RockBurstService {

    /**
     * 获取cjdy库（离层和锚杆）的服务类
     */
    @Autowired
    private RoofService roofService;

    /**
     * 实时检测
     * @return
     */
    @Override
    public NowDetectionRockBurstVo nowDetection(String mineCode, String companyCode) {
        //实时检测返回数据
        NowDetectionRockBurstVo nowDetectionRockBurstVo = new NowDetectionRockBurstVo();
        //钻孔应力异常数(暂时查的是异常表)
        Integer boreholeStressAnomaly = roofService.getBoreholeStressAnomaly();
        nowDetectionRockBurstVo.setBoreholeStressAnomaly(String.valueOf(boreholeStressAnomaly));
        //钻孔应力测点数
        Integer boreholeStressPoint=roofService.getBoreholeStressPoint();
        nowDetectionRockBurstVo.setBoreholeStressPoint(String.valueOf(boreholeStressPoint));

//        //地音异常数
//        Integer rockNoiseAnomaly=roofService.getRockNoiseAnomaly();
//        nowDetectionRockBurstVo.setRockNoiseAnomaly("---");
//
//        //地音测点数
//        Integer rockNoisePoint=roofService.getRockNoisePoint();
//        nowDetectionRockBurstVo.setRockNoisePoint(String.valueOf(rockNoisePoint));

        //电磁辐射异常数
        nowDetectionRockBurstVo.setElectromagneticRadiationAnomaly("---");
        //电磁辐射测点数
        Integer electromagneticRadiationPoint = roofService.getElectromagneticRadiationPoint();
        nowDetectionRockBurstVo.setElectromagneticRadiationPoint(String.valueOf(electromagneticRadiationPoint));

        //微震异常数
        nowDetectionRockBurstVo.setSlightShockAnomaly("---");

        //微震测点数
        Integer slightShockPoint=roofService.getSlightShockPoint();
        nowDetectionRockBurstVo.setSlightShockPoint(String.valueOf(slightShockPoint));

        return nowDetectionRockBurstVo;
    }

    /**
     * 风险统计
     * @param riskStatisticsRockBurstDto
     * @return
     */
    @Override
    public R riskStatisticsRockBurst(RiskStatisticsRockBurstDto riskStatisticsRockBurstDto) {
        //折线图返回格式

        //1-微震次数变化曲线，2-地音能量频次图
        if ("1".equals(riskStatisticsRockBurstDto.getLib())) {
            //微震次数变化曲线
            ChartVo  chartVo =roofService.riskStatisticsRockBurstQuake(riskStatisticsRockBurstDto.getNumber());
            return R.ok(chartVo);
        } else if ("2".equals(riskStatisticsRockBurstDto.getLib())) {
            //地音能量频次图
            ChartXTwoVo chartXTwoVo =roofService.riskStatisticsRockBurstBorehole(riskStatisticsRockBurstDto.getNumber());
            return R.ok(chartXTwoVo);
        }
        return R.ok();
    }

    /**
     * 今日报警处置情况
     * @return
     */
    @Override
    public List<TodayAlarmVo> todayAlarm() {
        List<TodayAlarmVo> list = new ArrayList<>();
        //钻孔应力报警（查询异常表内的数据）

        List<BoreholealarmAbnormalPo> boreholealarmAbnormalPoList = roofService.BoreholealarmAbnormalList();
        for (BoreholealarmAbnormalPo boreholealarmAbnormalPo : boreholealarmAbnormalPoList) {
            TodayAlarmVo todayAlarmVo = new TodayAlarmVo();
            //矿井
            if (StringUtils.isNotBlank(boreholealarmAbnormalPo.getMineCode())) {
                todayAlarmVo.setMine(boreholealarmAbnormalPo.getMineCode());
            } else {
                todayAlarmVo.setMine("---");
            }
            //报警类型
            todayAlarmVo.setAlarmType(ConstantSupport.BOREHOLE_STRESSMETER);

            //报警原因
            todayAlarmVo.setAlarmCause(boreholealarmAbnormalPo.getPointLocation()+boreholealarmAbnormalPo.getAbnormalType());

            if (boreholealarmAbnormalPo.getAbnormalStartTime() != null) {

                todayAlarmVo.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(boreholealarmAbnormalPo.getAbnormalStartTime()));
            } else {
                todayAlarmVo.setStartTime("---");
            }
            //开始时间


            //处置情况：True-已处置，false-未处置
            todayAlarmVo.setDispositionSituation(false);

            list.add(todayAlarmVo);
        }
        //地音报警

        //微震报警

        //巷道位移报警

        return list;
    }

    /**
     * 获取今日顶板离层报警列表
     * @param companyCode
     * @return
     */
    @Override
    public List<AbscissdataAlarmListVo> getTodayAbscissdataAlarmList(String companyCode) {
        if (StringUtils.isEmpty(companyCode)) {
            throw new HuaNengException(Constants.FAIL, "公司编码不可以为空");
        }
        //获取当前公司的所有煤矿
        SysOrganizationVo sysOrganizationVo = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY + companyCode);
        if (sysOrganizationVo == null) {
            throw new HuaNengException(Constants.FAIL, "无法获取当前公司的子煤矿信息，请检查词典"+companyCode);
        }
        List<AbscissdataAlarmListVo> voList = new ArrayList<>();
        for (SysOrganizationVo organizationVo : sysOrganizationVo.getSubCodeList()) {
            //遍历所有煤矿
            List<AbscissdataAlarmListVo> abscissAlarmList = roofService.getAbscissAlarmList(organizationVo.getOrgCode());
            for (AbscissdataAlarmListVo abscissdataAlarmListVo : abscissAlarmList) {
                //煤矿信息集合
                voList.add(abscissdataAlarmListVo);
            }

        }
        return voList;
    }

    /**
     * 获取今日锚杆索报警列表
     * @param companyCode
     * @return
     */
    @Override
    public List<AnchordataAlarmListVo> getTodayAnchordataAlarmList(String companyCode) {
        if (StringUtils.isEmpty(companyCode)) {
            throw new HuaNengException(Constants.FAIL, "公司编码不可以为空");
        }
        //获取当前公司的所有煤矿
        SysOrganizationVo sysOrganizationVo = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY + companyCode);
        if (sysOrganizationVo == null) {
            throw new HuaNengException(Constants.FAIL, "无法获取当前公司的子煤矿信息，请检查词典"+companyCode);
        }
        List<AnchordataAlarmListVo> voList = new ArrayList<>();
        for (SysOrganizationVo organizationVo : sysOrganizationVo.getSubCodeList()) {
            //遍历所有煤矿
            List<AnchordataAlarmListVo> anchordataAlarmList = roofService.getTodayAnchordataAlarmList(organizationVo.getOrgCode());
            for (AnchordataAlarmListVo anchordataAlarmListVo : anchordataAlarmList) {
                //煤矿信息集合
                voList.add(anchordataAlarmListVo);
            }
        }
        return voList;
    }

    /**
     * 获取今日支架报警列表
     * @param companyCode
     * @return
     */
    @Override
    public List<SupportdataAlarmListVo> getTodaySupportdataAlarmList(String companyCode) {
        if (StringUtils.isEmpty(companyCode)) {
            throw new HuaNengException(Constants.FAIL, "公司编码不可以为空");
        }
        //获取当前公司的所有煤矿
        SysOrganizationVo sysOrganizationVo = RedisUtils.getCacheObject(Constants.SYS_CODESUB_KEY + companyCode);
        if (sysOrganizationVo == null) {
            throw new HuaNengException(Constants.FAIL, "无法获取当前公司的子煤矿信息，请检查词典"+companyCode);
        }
        List<SupportdataAlarmListVo> voList = new ArrayList<>();
        for (SysOrganizationVo organizationVo : sysOrganizationVo.getSubCodeList()) {
            //遍历所有煤矿
            List<SupportdataAlarmListVo> supportdataAlarmListVos = roofService.getTodaySupportdataAlarmList(organizationVo.getOrgCode());
            for (SupportdataAlarmListVo supportdataAlarmListVo : supportdataAlarmListVos) {
                //煤矿信息集合
                voList.add(supportdataAlarmListVo);
            }
        }
        return voList;
    }
}
